﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace UsersForms
{
    public partial class Form3 : Form
    {
        public static Form3 StrartForm { get; set; }

        public Form3()
        {
            InitializeComponent();

            //启动窗体的时候隐藏窗体
            this.ShowInTaskbar = false;
            this.WindowState = FormWindowState.Minimized;
        }

        private void Form3_Load(object sender, EventArgs e)
        {
            StrartForm = this;//存储当前实例

            Form1 enter = new Form1();
            enter.Show();//进入登入窗口

            toolStripStatusLabel3.Text = DateTime.Now.ToString("yyyy/MM/dd HH:mm:ss");
            timer1.Start();

            string sql = "select * from Articles";

            dataGridView1.DataSource = DBHelper.Select(sql);

            dataGridView1.SelectionMode = DataGridViewSelectionMode.FullRowSelect;

            dataGridView1.ReadOnly = false;

            dataGridView1.AllowUserToAddRows = false;

        }
        /// <summary>
        /// 查询
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void button1_Click(object sender, EventArgs e)
        {
            if (comboBox1.SelectedItem != null)
            {
                if (comboBox1.SelectedItem.ToString() == "Title")
                {
                    string sql = string.Format("select * from Articles where Title like '%{0}%'", textBox1.Text);

                    dataGridView1.DataSource = DBHelper.Select(sql);
                }
                else if (comboBox1.SelectedItem.ToString() == "Author")
                {
                    string sql = string.Format("select * from Articles where Author like '%{0}%'", textBox1.Text);

                    dataGridView1.DataSource = DBHelper.Select(sql);
                }
            }
            else
            {
                string sql = string.Format("select * from Articles", textBox1.Text);

                dataGridView1.DataSource = DBHelper.Select(sql);
            }


        }

        /// <summary>
        /// 添加
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void button2_Click(object sender, EventArgs e)
        {
            Form4 form4 = new Form4();
            form4.Show();
        }
        /// <summary>
        /// 更新
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void button3_Click(object sender, EventArgs e)
        {
            if (dataGridView1.SelectedRows.Count == 1)
            {
                var id = dataGridView1.SelectedCells[0].Value.ToString();
                var title = dataGridView1.SelectedCells[1].Value.ToString();
                var author = dataGridView1.SelectedCells[2].Value.ToString();

                Form4 form4 = new Form4(title, author, id);
                form4.Show();
            }
            else
            {
                MessageBox.Show("请选择一行", "提示");
            }
        }
        /// <summary>
        /// 删除
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void button4_Click(object sender, EventArgs e)
        {
            if (dataGridView1.SelectedRows.Count == 1)
            {
                var id = dataGridView1.SelectedCells[0].Value.ToString();

                var len = MessageBox.Show("确定要删除当前选中的行吗？", "提示", MessageBoxButtons.OKCancel, MessageBoxIcon.Warning);

                if (DialogResult.OK.Equals(len))
                {

                    string sql = string.Format("delete Articles where id={0}", id);

                    var b = DBHelper.DeleteUpdateInsert(sql);


                    if (b >= 1)
                    {
                        MessageBox.Show("删除成功！！");

                        string sql2 = string.Format("select * from Articles where Title like '%{0}%'", textBox1.Text);

                        dataGridView1.DataSource = DBHelper.Select(sql2);
                    }
                }
            }
        }

        private void timer1_Tick(object sender, EventArgs e)
        {
            toolStripStatusLabel3.Text = DateTime.Now.ToString("yyyy/MM/dd HH:mm:ss");
        }
    }
}
